
Ext.onReady(function(){
	
	Ext.QuickTips.init();

	// Disable browser right click
	Ext.fly(document.body).on('contextmenu', function(e, target) {
		e.preventDefault();
	});	

Ext.tree.XDomainTreeLoader = function(config){
  Ext.tree.XDomainTreeLoader.superclass.constructor.call(this, config);
};

Ext.extend(Ext.tree.XDomainTreeLoader, Ext.tree.TreeLoader, {
    getParams: function(node){
      var bp = this.baseParams;
      bp.node = node.id;
      return bp;
    },

    requestData: function(node, callback){
      var url = this.url || this.dataUrl;
      if(url && !this.proxy) {
        this.proxy = new Ext.data.ScriptTagProxy({url: this.url}); 
      }
      this.proxy.url = url;
      
      if(this.fireEvent("beforeload", this, node, callback) !== false) { 
   //     var p = this.getParams(node);
      /*  var reader = new Ext.data.JsonReader({
										root:'data'
									},[	'taxonID'
									, 	'datasetID'
									,	'higherTaxonID'
									,	'nameID'
									,	'scientificName'
									,	'rank'
									,	'isSynonym'
									,	'taxonomicStatus'
									,	'numChildren'
									,	'numDescendants'
									,	'rating']);*/
        this.proxy.load( reader, this.handleResponse, this, {cb:callback,node:node});
      }
      else{
        // if the load is cancelled, make sure we notify the node that we are done
        if(typeof callback == "function"){
          callback();
        }
      }
    },

    processResponse: function(data, node, callback){
      try {
       	 	var o = data;
        	var o = eval("(" + json + ")");
				o = o.data;
				node.beginUpdate();
				for (var i = 0, len = o.length; i < len; i++) {
					var n = this.createNode(o[i]);
					n.text = o[i].scientificName;
					n.attributes.type = 'gbif.clb';
					n.attributes.taxonID = o[i].taxonID;
					n.attributes.datasetID = o[i].datasetID;
					n.attributes.higherTaxonID = o[i].higherTaxonID;
					n.attributes.nameID = o[i].nameID;
					n.attributes.scientificName = o[i].scientificName;
					n.attributes.rank = o[i].rank;
					if (o[i].rank == 'kingdom' || o[i].rank == 'species'){
						n.attributes.checked = false;
					}
					n.attributes.isSynonym = o[i].isSynonym;
					n.attributes.taxonomicStatus = o[i].taxonomicStatus;
					n.attributes.numChildren = o[i].numChildren;
					if (o[i].numChildren == 0){
						n.leaf=true;
					} else {
						n.leaf=false;
					}
					n.attributes.numDescendants=o[i].numDescendants;
					n.attributes.rating=o[i].rating;
					if (n) {
						node.appendChild(n);
					}
				}
				node.endUpdate();
				if (typeof callback == "function") {
					callback(this, node);
				}
      }
      catch(e) {
        this.handleFailure(data);
      }
    },

    handleResponse: function(data, arg, isSuccess) {
      console.log("data: %o\narg: %o\nisSuccess: %o", data, arg, isSuccess);
      data = data.records;
      var o;
      if(this.rootField) {
        o = data[this.rootField];
      }
      else {
        o = data;
      }
      this.processResponse(o, arg.node, arg.cb);
      this.fireEvent("load", this, arg.node, data);
    },

    handleFailure: function(data, node, callback){
      this.transId = false;
      this.fireEvent("loadexception", this, node, data);
      if(typeof callback == "function"){
        callback(this, node);
      }
    }
});


demotree =new Ext.tree.TreePanel({
					height: 400
				,	title:'Demo Tree'
				,	border:true
	            ,   width: 400
				,	renderTo: Ext.getBody()
				,	useArrows: false
				,	border: false
				,	rootVisible: true
				,	root: new Ext.tree.AsyncTreeNode({
								text: 'gbif.clb'
							,	draggable: false
							,	expanded:false
						})
				,	autoScroll: true	
				,	loader: new Ext.tree.XDomainTreeLoader ({
							 proxy: new Ext.data.HttpProxy({
	    	  						  url: 'http://ecat-dev.gbif.org/ws/nav/'
	        					})
							//dataUrl: 'http://ecat-dev.gbif.org/ws/nav/'
						})
	});

});
